
import wx
import wx.lib.agw.customtreectrl as CT

class GeneSnpChecklist(wx.lib.agw.customtreectrl.CustomTreeCtrl):
    def __init__(self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.DefaultSize, style =CT.TR_DEFAULT_STYLE):

        #wx.Frame.__init__(self, parent, -1, "CustomTreeCtrl Demo")        

        # Create a CustomTreeCtrl instance
        wx.lib.agw.customtreectrl.CustomTreeCtrl.__init__(self, parent, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 
                                                          style=style | wx.TR_HIDE_ROOT | CT.TR_AUTO_CHECK_CHILD | wx.TR_HAS_BUTTONS | 
                                                          wx.TR_LINES_AT_ROOT )#| CT.TR_ROW_LINES)#| wx.TR_TWIST_BUTTONS)  | CT.TR_AUTO_CHECK_PARENT
        #custom_tree.SetIndent(100)
        # Add a root node to it
        self.root = self.AddRoot("The Root Item")            
#        root2 = custom_tree.AddRoot("The Root Item")            

        #self.SetSpacing(100)

        # Create an image list to add icons next to an item
        il = wx.ImageList(40, 40)
        #fldridx     = il.Add(wx.ArtProvider_GetBitmap((wx.ART_FOLDER,      wx.ART_OTHER, 16)))
        #fldropenidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN,   wx.ART_OTHER, 16))
        #fileidx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, 16))
        self.snp_icon     = il.Add(wx.Bitmap("images/snp_diagram_icon2_20x35.png"))
        #self.gene_icon = il.Add(wx.Bitmap("images/dna_small_icon.jpg"))
        self.gene_icon = il.Add(wx.Bitmap("images/gene_icon_vert_45x27.jpg"))
        #fileidx     = il.Add(wx.Bitmap("images/project_icons/music_16.png"))
        #wx.ImageList.a
        self.SetImageList(il)
        
        #custom_tree.SetItemImage(root, gene_icon, wx.TreeItemIcon_Normal)
        #custom_tree.SetItemImage(root, gene_icon, wx.TreeItemIcon_Expanded)
        #custom_tree.AutoCheckChild(root, True)
        genes = ["ACER1", "ACER2", "STAT6", "PRACTA", "META7"]
        
        for gene in genes:
            gene_entry = self.AppendItem(self.root, "%s" % gene, ct_type=1)
            
            self.SetItemImage(gene_entry, self.gene_icon, wx.TreeItemIcon_Normal)
            self.SetItemImage(gene_entry, self.gene_icon, wx.TreeItemIcon_Expanded)
            #custom_tree.AutoCheckChild(gene_entry, False)
            #custom_tree.Che AutoCheckChild(gene_entry, False)
            for y in range(5):
                snp_entry = self.AppendItem(gene_entry, "rs%s" % y, ct_type=1)
                self.SetItemImage(snp_entry, self.snp_icon, wx.TreeItemIcon_Normal)
                self.SetItemImage(snp_entry, self.snp_icon, wx.TreeItemIcon_Expanded)
                #custom_tree.CheckItem(snp_entry, True)#SetItemImage(snp_entry, snp_icon, wx.TreeItemIcon_Expanded)
#            custom_tree.AutoToggleChild(gene_entry)
            #custom_tree.CheckChilds(gene_entry, True)
            #custom_tree.
            

#                for z in range(5):
#                    item = custom_tree.AppendItem(last,  "item %d-%s-%d" % (x, chr(ord("a")+y), z))
#                    custom_tree.SetItemImage(item, fileidx, wx.TreeItemIcon_Normal)
#                    custom_tree.SetItemImage(item, fldridx, wx.TreeItemIcon_Selected)
        #custom_tree.Expand(root)
        #self.Bind(wx.EVT_TREE_ITEM_, self.OnGeneChecked)
        #self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnGeneExpanded)
    
    
    def Clear(self):
        self.DeleteChildren(self.root)
    
    def add_genes(self, gene_names):
        for gene in gene_names:
            
            gene_entry = self.AppendItem(self.root, gene, ct_type=1)
            
            self.SetItemImage(gene_entry, self.gene_icon, wx.TreeItemIcon_Normal)
            self.SetItemImage(gene_entry, self.gene_icon, wx.TreeItemIcon_Expanded)
            gene_entry.SetBold(True)
            none_entry = self.AppendItem(gene_entry, "none", ct_type=1)
            
            #self.SetItemImage(snp_entry, self.snp_icon, wx.TreeItemIcon_Normal)
            
            
    def add_snps(self, snp_names, parent_gene):
        #print parent_gene.GetLabel()
        self.DeleteChildren(parent_gene)
        for snp in snp_names:
            snp_entry = self.AppendItem(parent_gene, snp, ct_type=1)
            self.SetItemImage(snp_entry, self.snp_icon, wx.TreeItemIcon_Normal)
            self.SetItemImage(snp_entry, self.snp_icon, wx.TreeItemIcon_Expanded)
            #snp_entry.SetSpacing(100)
            #snp_entry.SetIndent(100)
    #    g
        
  #  def gene    
    #def OnGeneChecked(self, event):
    #    print "gene checked"
    #def OnGeneExpanded(self, event):
        #print "expand"
        #event.Skip()
        
            
# our normal wxApp-derived class, as usual
if __name__ == "__main__":
    app = wx.PySimpleApp()
    
    frame = wx.Frame(None)
    
    gene_checklist = GeneSnpChecklist(frame)
    
    frame.Show()
    
    app.MainLoop()

